package com.magupe.im.web.client.common.freemarker;

import org.springframework.beans.BeansException;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.BeanFactoryAware;
import org.springframework.boot.context.properties.ConfigurationProperties;

import java.util.Collections;
import java.util.HashMap;
import java.util.Map;

/**
 * @author xing
 */
@ConfigurationProperties(prefix = "freemarker.variables")
public class FreeMarkerVariablesProperties implements BeanFactoryAware {
	
	private BeanFactory beanFactory;
	
	private Map<String,String> directive = new HashMap<>();
	
	private Map<String,String> cons = new HashMap<>();

	public Map<String, String> getDirective() {
		return directive;
	}
	
	public Map<String,String> getCons(){
		return cons;
	}
	
	public Map<String,Object> getDirectiveBeanMap(){
		Map<String,String> directives = getDirective();
		if(directives.isEmpty()) {
			return Collections.emptyMap();
		}
		Map<String, Object> resultMap = new HashMap<>(directives.size());
		String key, value;
		for (Map.Entry<String, String> entry : directives.entrySet()) {
			key = entry.getKey();
			value = entry.getValue();
			resultMap.put(key, beanFactory.getBean(value, Object.class));
		}
		return resultMap;
	}

	public void setDirective(Map<String, String> directive) {
		this.directive = directive;
	}
	
	public void setCons(Map<String,String> cons){
		this.cons = cons;
	}

	@Override
	public void setBeanFactory(BeanFactory beanFactory) throws BeansException {
		this.beanFactory = beanFactory;
	}
	
}
